import fetch from 'common/src/utils/fetch';
import cryp from  'common/src/utils/secret';

function selectApproFlow(data) {
    return fetch({
        url: '/api/workflow/approval/selectApproFlow' + getParams(data),
        method: 'get'
    });
}

function selectApproFlowList(data) {
    return fetch({
        url: '/api/workflow/approval/selectApproFlowList',
        method: 'post',
        data
    });
}

function findUserInfoByIdType(data) {
    return fetch({
        url: '/api/studentservice/studentInfo/findUserInfoByIdType' + getParams(data,true),
        method: 'get',
    });
}

function approvalThrough(data) {
    return fetch({
        url: '/api/workflow/approval/through',
        method: 'post',
        data
    });
}

function approvalReject(data) {
    return fetch({
        url: '/api/workflow/approval/reject',
        method: 'post',
        data
    });
}

/**
 * 
 * @param {*} params 
 * @param {*} requireEncryption 是否需要加密
 */
function getParams(params,requireEncryption) {
    var paramsData = [];
    for (const key in params) {
        params[key] &&
            paramsData.push(
                requireEncryption?
                key + '=' + cryp.encrypt(params[key]): key + '=' + params[key]
            );
    }
    if (paramsData.length)
        return '?' + paramsData.join('&');
    else
        return '';
}

function getFacultyParams(params) {
    if(!params || !params.length) return '';
    var paramsData = [];
    params.forEach(item => {
        paramsData.push(
            'faculty=' + item
        );
    })
    if (paramsData.length)
        return '?' + paramsData.join('&');
    else
        return '';
}


const permissionApi = {
    'selectApproFlow': selectApproFlow,
    'selectApproFlowList': selectApproFlowList,
    'findUserInfoByIdType': findUserInfoByIdType,
    'approvalThrough': approvalThrough,
    'approvalReject': approvalReject
}

export default permissionApi;